using Assets.Scripts.GDK;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Brick
{
	public class YuLanPanel : gdk_base_panel
	{
		private List<GameObject> enemyitems;

		public Transform content;

		public int selectIndex;

		public Text text;

		public Sprite[] type1;

		public Sprite[] type2;

		public Sprite[] type3;

		public GameObject btnAd;

		private int iHaveBallNum = 50;

		public override void OnLoad()
		{
		}

		public override void OnStart()
		{
			GDK.ModuleManger.Module<MuzzuleBoxModule>().SetCanShoot(bShoot: false);
			initData();
			//预览窗口
			//if (!GameManager.getFuncOpen("yulanad"))
			//{
			//	btnAd.SetActive(value: false);
			//}
			OnNormalClick();

		}

		private void initData()
		{
			//关卡数据
			selectIndex = GameData.selectIndex;
			//如果关卡过高 提高小球的数量确保能够通过
			if (selectIndex >= 100)
			{
				iHaveBallNum = 60;
			}

			text.text = "level: " + selectIndex;  //"Level: " + selectIndex; //�ĳ����� --shicy


			if (EnemyPool.GetInstance().GetObjCount("Prefabs/Items/Yulan/yulanitem") == 0)
			{
				EnemyPool.GetInstance().CreateObj(150, "Prefabs/Items/Yulan/yulanitem", "Prefabs/Items/Yulan/yulanitem", PoolPlayer.Instance.GetObj());
			}

			enemyitems = new List<GameObject>();

			LevelData levelData = GDK.ModuleManger.Module<EnemyBoxModule>().loadGameConfigJson(selectIndex);    ///问题


			//匹配成功从对象池中获取物体减少性能消耗
			if (levelData != null)
			{
				foreach (DataItem item in levelData.Stage)
				{
					int num = (item.ID - 1) / 11;
					GameObject obj = EnemyPool.GetInstance().GetObj("Prefabs/Items/Yulan/yulanitem", "Prefabs/Items/Yulan/yulanitem");
					int num2 = item.ID - 1;
					int num3 = 20 - num2 / 11;
					float num4 = num2 % 11 + 1;
					obj.transform.SetParent(content);
					obj.transform.localScale = Vector3.one * 0.6f;
					float x = -230f + num4 * 61f * 0.6f;
					obj.transform.localPosition = new Vector3(x, -200f + (float)(num3 * 61) * 0.6f, 0f);
					Sprite sp = null;
					if (item.type == 1)
					{
						sp = type1[item.typeID];
					}
					else if (item.type == 2)
					{
						sp = type2[item.typeID];
					}
					else if (item.type == 3)
					{
						int num5 = item.typeID / 5;
						int num6 = item.typeID % 5;
						string id = "UI/Brick/tq_brick_3_" + num5 + "_" + num6;
						Sprite sprite = gdk_game_manager.loadSprite(id);
						sp = sprite;
					}
					obj.GetComponent<YuLanItem>().setSp(sp);
					enemyitems.Add(obj);
				}
			}
		}

		public void onShowEvent()
		{
			initData();
			base.gameObject.SetActive(value: true);
		}

		public void onHideEvent()
		{
			base.gameObject.SetActive(value: false);
		}

		public void OnUseClick()
		{
			gdk_game_manager.PlaySound(AudioID.click);
			GDK.ModuleManger.Module<AdvansBoxMoudle>().PlayAD(Advans.YULANADDBALL, "4", delegate
			{
				gdk_game_manager.DispachEvent(EventID.GAME_PLAY_START, selectIndex);
				gdk_game_manager.DispachEvent(EventID.SHOW_PLAY_MENU);
				gdk_game_manager.DispachEvent(EventID.GAME_CREATE_BALL, iHaveBallNum + 3);
				RecykeEnemys();
				close();
			}, delegate
			{
				gdk_game_manager.message("Advertisement is being prepared");
			});
		}

        //直接开始游戏（无广告）
        public void OnNormalClick()
		{
			gdk_game_manager.PlaySound(AudioID.click);  //声音

			gdk_game_manager.DispachEvent(EventID.GAME_PLAY_START, selectIndex);

			gdk_game_manager.DispachEvent(EventID.SHOW_PLAY_MENU);

			gdk_game_manager.DispachEvent(EventID.GAME_CREATE_BALL, iHaveBallNum);
			RecykeEnemys();
			close();
			
		}

        //回收敌人对象  
        public void RecykeEnemys()
		{
			for (int i = 0; i < enemyitems.Count; i++)
			{
				GameObject gameObject = enemyitems[i];
				if (gameObject != null)
				{
					EnemyPool.GetInstance().RecycleObj(gameObject);
					enemyitems.Remove(gameObject);
				}
			}
		}
	}
}

